package DataStrucures;

public class HashDemo {
    public static void main(String[] args) {
        HashTab hashTab=new HashTab(5);
        //测试案例
        Emp emp0=new Emp(0,"人才0");
        Emp emp3=new Emp(3,"人才3");
        Emp emp4=new Emp(4,"人才4");
        Emp emp=new Emp(1,"人才");
        Emp emp1=new Emp(2,"人才1");
        Emp emp2=new Emp(3,"人才2");
        Emp emp5=new Emp(5,"人才5");
        Emp emp6=new Emp(9,"人才9");
        hashTab.add(emp);
        hashTab.add(emp1);
        hashTab.add(emp2);
        hashTab.add(emp0);
        hashTab.add(emp3);
        hashTab.add(emp4);
        hashTab.add(emp5);
        hashTab.add(emp6);
        hashTab.show();
        System.out.println();
        hashTab.find(5);
        System.out.println();
        hashTab.del(1);
        System.out.println();
        hashTab.show();


    }

}
class Emp{
    public int id;
    public String name;
    public Emp next;

    public Emp(int id, String name) {
        this.id = id;
        this.name = name;
    }
}
class EmpListKeyList{
    private Emp head;

    public void add(Emp emp){
        if(head==null){
            head=emp;
            return;
        }
        Emp temp=head;
        while (true){
            if(temp.next==null){

                break;
            }
            temp=temp.next;
        }
        temp.next=emp;
    }
    public void show(){
        if(head==null){
            System.out.println("信息为空");
            return;
        }
        Emp temp=head;
        while (true){
            System.out.print(temp.id+temp.name+" ");
            if(temp.next==null){
                System.out.println();
                break;
            }
            temp=temp.next;
        }
    }
    public void find(int id){
        Emp temp=head;
        while (true){
            if(temp.id==id){
                System.out.print(temp.id+temp.name+" ");
                break;
            }
            if(temp.next==null){
                System.out.println("查无此人");
                break;
            }
            temp=temp.next;
        }
    }
    public int del(int id){
        Emp temp=head;
        if(head.id==id){
            return -1;
        }
        while (true){
            if(temp.next.id==id){
                temp.next=temp.next.next;
                break;
            }
            if(temp.next.next==null){
                System.out.println("无效操作");
                break;
            }
            temp=temp.next;
        }
        return 1;
    }
}
class HashTab{
    private EmpListKeyList[] empListKeyLists;
    private int size;
    public HashTab(int size){
        this.size=size;
        empListKeyLists = new EmpListKeyList[size];
        for (int i = 0; i < size; i++) {//给每个数组都NEW一个链表头
            empListKeyLists[i]=new EmpListKeyList();
        }

    }
    public void add(Emp emp){
        int No=hashFun(emp.id);
        empListKeyLists[No].add(emp);
    }
    public void show(){
        for (int i = 0; i < size; i++) {
            empListKeyLists[i].show();
        }
    }
    public void find(int id){
        int No=hashFun(id);
        empListKeyLists[No].find(id);
    }
    public void del(int id){
        int No=hashFun(id);
        if(empListKeyLists[No].del(id)==-1){
            empListKeyLists[No]=new EmpListKeyList();
        }
    }

    public int hashFun(int id){
        return id%size;
    }
}
